<%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.MediaVaultTest.UserStore" %>
<%@ page import="com.MediaVaultTest.Media" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="com.google.appengine.api.datastore.PreparedQuery" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="javax.jdo.JDOHelper" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.PersistenceManagerFactory" %>
<%@ page import="com.MediaVaultTest.PMF" %>
<%@ page import="javax.jdo.JDOObjectNotFoundException" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="com.MediaVaultTest.Format" %>
<%@ page import="com.MediaVaultTest.QuotaLogic" %>
<%@ page import="com.MediaVaultTest.GetAdds" %>

<%
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	GetAdds getAdds = new GetAdds();

%>

<!DOCTYPE HTML>
<html>

<head>
  <title>QUT Media Vault</title>
  <meta name="description" content="website description" />
  <meta name="keywords" content="website keywords, website keywords" />
  <meta http-equiv="content-type" content="text/html; charset=windows-1252" />
  <link rel="stylesheet" type="text/css" href="style/style.css" />
</head>

<body>
  <%
  	//Setup user service and data store
	 UserService userService = UserServiceFactory.getUserService();
	 User user = userService.getCurrentUser();
	 
	 //If user logged in
	 if (user != null) {
		 DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		 PersistenceManager pm = PMF.get().getPersistenceManager();
		 
		 //Check if user system
		 String email =  user.getEmail();
		 
		 UserStore userStore = null;
		 Key key = KeyFactory.createKey(UserStore.class.getSimpleName(), email);
		 try { 
			 userStore = pm.getObjectById(UserStore.class, key);
			 //See if anything is returned\
		 } catch (JDOObjectNotFoundException e) {
			 userStore = null;
		 }
		 
		
		 //if not create them
		 if(userStore==null){
			 int up = 0;
			 int down = 0;
			 int store = 0;
			 userStore = new UserStore(email, up, down, store);
			 //userStore = new UserStore();
			 userStore.setKey(key);
			 try {
				 pm.makePersistent(userStore);
			 } finally {
				 
			 }
		} else {
			
		}
	
  %>
  <div id="main">
    <div id="header">
      <div id="logo">
        <div id="logo_text">
          <!-- class="logo_colour", allows you to change the colour of the text -->
          <h1><a href="index.html">Media Vault<span class="logo_colour">QUT</span></a></h1>
          <h2>Media Centre. Currently in Alpha</h2></h2>
        </div>
        <div id="ads_top">
			<%=
	        	getAdds.getTopBanner()
	        %>
        </div>
      </div>
      <div id="menubar">
        <ul id="menu">
        <!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
        <li><a href="index.jsp">General</a></li>
		  <li><a href="photos.jsp">Photos</a></li>
        <li><a href="music.jsp">Music</a></li>
        <li><a href="movies.jsp">Movies</a></li>
        <li><a href="tv_shows.jsp">TV Shows</a></li>
		  <%if (userService.isUserAdmin()) {%>
          
		  <li><a href="adminPanel.jsp">Admin Panel</a></li>
		  <%} %>
		  <li><a href="userPanel.jsp">User Panel</a></li>
		  <li><a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">Sign Out</a></li>
        </ul>
      </div>
    </div>
    <%
	String stringKey = request.getParameter("key");
        
    //Default thumbnail
    String image = "style/link.png";
    
    if(stringKey==null){
    	
    } else {
    	Key mediaKey = KeyFactory.stringToKey(stringKey);
    	//Get the format
    	Media media = pm.getObjectById(Media.class, mediaKey);
    	if(!media.getUserStore().equals(userStore)){
    		response.sendRedirect("/index.jsp?msg=Get Out!");
    	}
    	Format format = media.getFirstFormat();
    	
    	//Get cover, if got replace default
    	String temp = media.getCoverForTrans();
    	if(temp!=""){
    		image = "/getThumbnail?key="+temp;
    	}
    	
    	 %>
    <div id="site_content">
      <div class="left_sidebar_view">
      	<h3>Upload Thumbnail</h3>
        <table>
        <form action="<%= blobstoreService.createUploadUrl("/uploadThumbnail") %>" method="post" enctype="multipart/form-data">
        <tr><td><img src="<%=image %>" width="96px" height="96px"></td></tr>
        <tr><td>
        <input type="file" name="myFile">
        <input type="hidden" name="media" value="<%= stringKey%>">
        <input type="Submit" value="Submit">
        </td></tr>
        </form></table>
        <h3>Upload additional formats</h3>
			<form action="<%= blobstoreService.createUploadUrl("/uploadFormat") %>" method="post" enctype="multipart/form-data">
		            <input type="file" name="myFile"><br>
		            <input type="hidden" name="media" value="<%= stringKey%>">
		            <input type="submit" value="Submit">
		        </form>
        <table>
        <% 
        LinkedList<Format> formats = media.getFormatList();
        int numFormats = formats.size();
        for(int i = 0; i < numFormats; i++){
        	format = formats.get(i);
       		 %>
			<tr><td>Format <%= i+1 %></td>
			<td><a href='/stream.jsp?key=<%= format.getKeyForTrans()  %>'>
			Stream</a></td>
			<td><a href='/serve?key=<%= format.getKeyForTrans()  %>'>
			Download</a></td><td><a href='/delete?key=<%= format.getKeyForTrans()%>'>
		Delete</a></td></tr>
		<% 
        }
        %>
      </table>  
      </div>
      <div class="sidebar">
      <h1>Quota Remaining</h1>
      <table>
      <% QuotaLogic quotaLogic = new QuotaLogic(); %>
      <tr><td>Upload</td><td><%= quotaLogic.getMBUploadRemaining(userStore) %>MB</td></tr>
      <tr><td>Download</td><td><%= quotaLogic.getMBDownloadRemaining(userStore) %>MB</td></tr>
      <tr><td>Storage</td><td><%= quotaLogic.getMBStoreRemaining(userStore) %>MB</td></tr>
      </table>
      	
        <div id="ads_left">
	        <%=
	        	getAdds.getRightBanner()
	        %>
        </div>
      </div>
      <div id="contentView">
    	<h1>Edit Meta Information</h1>
    	
    	<form action="/editInfo" method="post">
    	<input type="hidden" name=theKey size= 50 value="<%=stringKey %>">
    	<table>
    		<tr><td>Title</td><td width="250px"><input type="text" name="title" size="30" value="<%= media.getTitle()%>"></td></tr>
    		<!--
    		 * Change this to a drop down box, providing photo, music, tv, movie and general
    		 -->
    		<tr><td>Media Type</td><td>
    		<select name="mediaType">
    			<% LinkedList<String> types = new LinkedList<String>();
    			types.add("Photo");
    			types.add("Music");
    			types.add("Movie");
    			types.add("TV");
    			for(int i = 0; i < types.size(); i++){
    				if(types.get(i).equals(media.getMediaType())){
    					%><option selected="true"><%= types.get(i) %></option> <%
    				} else {
    					%><option><%= types.get(i) %></option> <%
    				}
    			}	%>
        	</select></td></tr>
    		<tr><td>Language</td><td><input type="text" name="language" size="30" value="<%= media.getLanguage()%>"></td></tr>
    		<tr><td>Album</td><td><input type="text" name="album" size="30" value="<%= media.getAlbum()%>"></td></tr>
    		<tr><td>Artist</td><td><input type="text" name="artist" size="30" value="<%= media.getArtist()%>"></td></tr>
    		<tr><td>Genre</td><td><input type="text" name="genre" size="30" value="<%= media.getGenre()%>"></td></tr>
    		<tr><td>Series</td><td><input type="text" name="series" size="30" value="<%= media.getSeries()%>"></td></tr>
    		<tr><td>Rating</td><td><input type="text" name="rating" size="30" value="<%= media.getRating()%>"></td></tr>
    		<tr><td></td><td><input type="submit" value="Submit" size="30">    <input type="reset" size ="50"></td></tr>
        </table>
        </form>
        <%
    }
		%>
      </div>
    </div>
    <div id="footer">
      <p><a href="/">Home</a> | <a href="/">LOL Cats</a> | <a href="/">Contact Us</a></p>
      <p>Copyright &copy; simplestyle_5 | <a href="http://validator.w3.org/check?uri=referer">HTML5</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> | <a href="http://www.html5webtemplates.co.uk">design dervived from HTML5webtemplates.co.uk</a></p>
    </div>
  </div>
  <%
  pm.close();
    } else { 
  %>
        <p>Hello!
		<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
		to access media vault.</p>
  <% } %>
</body>
</html>
